package com.example.ymm.modules1.controller.sys;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.user.DbChatGroup;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.service.dd.RobotGroupMessagesService;
import com.example.ymm.modules1.service.dto.DbChatGroupDto;
import com.example.ymm.modules1.service.dto.DbChatGroupQueryCriteria;
import com.example.ymm.modules1.service.user.DbChatGroupDeptService;
import com.example.ymm.modules1.service.user.DbChatGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/chatGroup")
@Api(tags = "pc端群管理")
@Slf4j
@CrossOrigin
public class SysChatGroupController extends BaseController {

    @Autowired
    DbChatGroupService dbChatGroupService;

    @Autowired
    RobotGroupMessagesService robotGroupMessagesService;

    @Autowired
    DbChatGroupDeptService dbChatGroupDeptService;

    @ApiOperation(value = "列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult list(@Valid DbChatGroupQueryCriteria criteria, YmPageDto ymPage) {
        try {
            TableList<DbChatGroup> list = dbChatGroupService.list(criteria, ymPage);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "下拉列表")
    @RequestMapping(value = "/allChatGroupList", method = RequestMethod.POST)
    public CommonResult allChatGroupList(@Valid DbChatGroupQueryCriteria criteria, YmPageDto ymPage) {
        try {
            List<DbChatGroup> list = dbChatGroupService.queryAll(criteria);
            return CommonResult.success(list, "获取成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

        @ApiOperation(value = "编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public CommonResult edit(DbChatGroupDto dto) {
        try{

            dbChatGroupService.editFn(dto);

            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/createGroupChat", method = {RequestMethod.POST})
    @ApiOperation(value = "创建群聊", notes = "创建群聊")
    public CommonResult createGroupChat(@Valid DbChatGroupDto group) {
        try {
            DbChatGroup groupChat = dbChatGroupService.createGroupChat(group);
            return CommonResult.success(groupChat);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }


    @RequestMapping(value = "/queryGroupChatUsers", method = {RequestMethod.POST})
    @ApiOperation(value = "查询群成员", notes = "查询群成员")
    public CommonResult queryGroupChatUsers(@Valid DbChatGroupDto group) {
        try {
            List<DbUser> dbUsers = dbChatGroupService.queryGroupChatUsers(group.getChatid());
            return CommonResult.success(dbUsers);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }

    }

    @ApiOperation(value = "详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public CommonResult detail(@ApiParam(value = "数据主键") @RequestParam(required = true) String openConversationId) {
        try {
            DbChatGroup group = dbChatGroupService.queryGroupChatInfo(openConversationId);

            return CommonResult.success(group);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@ApiParam(value = "数据主键") @RequestParam(required = true) String openConversationId) {
        try {
            dbChatGroupService.removeById(openConversationId);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "给群聊设置部门")
    @RequestMapping(value = "/setDepts", method = RequestMethod.POST)
    public CommonResult setDepts(String openConversationId,String deptIds,String deptNames) {
        try {
            dbChatGroupDeptService.setDepts(openConversationId,deptIds,deptNames);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "获取群聊设置部门")
    @RequestMapping(value = "/getDeptIds", method = RequestMethod.POST)
    public CommonResult getDeptIds(String openConversationId) {
        try {
            List<String> deptIds = dbChatGroupDeptService.getDeptIds(openConversationId);
            return CommonResult.success(deptIds);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

}
